//
//  DCIMSettingViewController.m
//  xmppBase
//
//  Created by striby on 16/1/7.
//  Copyright © 2016年 com.dc. All rights reserved.
//

#import "DCIMSettingViewController.h"
#import "DCMeInfoTableViewCell.h"
#import "DCSettingTableViewCell.h"

@interface DCIMSettingViewController ()

@end

@implementation DCIMSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, DCDeviceWidth, DCDeviceHeight)];
    self.tableView.delegate=self;
    self.tableView.dataSource =self;
    self.tableView.backgroundColor=[UIColor groupTableViewBackgroundColor];

    
    [self.view addSubview:self.tableView];

    [self.tableView setContentInset:UIEdgeInsetsMake(0, 0, TabBarHeight, 0)];
    
    [self.tableView setShowsVerticalScrollIndicator:NO];
    
    self.tableView.separatorInset = UIEdgeInsetsZero;
    
    self.title = @"我";
    UIView * bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 90)];
    
    UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, bottomView.frame.size.width, 20)];
    
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    
    // app版本
    NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    
    label.font = [UIFont systemFontOfSize:13];
    label.text = [NSString stringWithFormat:@"当前版本:%@",app_Version];
    label.textColor = [UIColor darkGrayColor];
    label.textAlignment = NSTextAlignmentCenter;
    
    [bottomView addSubview:label];
    
    
    UIButton * btnLogout = [[UIButton alloc]initWithFrame:CGRectMake(20, 25/2+label.frame.size.height, bottomView.frame.size.width-40, 45)];
    
    btnLogout.backgroundColor = [UIColor redColor];
    
    [btnLogout setTitle:@"注销" forState:UIControlStateNormal];
    [btnLogout setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [bottomView addSubview:btnLogout];
    [btnLogout addTarget:self action:@selector(logout) forControlEvents:UIControlEventTouchUpInside];
    [self.tableView setTableFooterView:bottomView];
  
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(reloadMyInfo) name:kXmppMyInfoChanged object:nil];
}

#pragma mark - 事件
-(void)reloadMyInfo
{
    
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];
}

-(void)logout
{
    [[DCXMPPTool shareInstance]logoutXmppHost];
}


-(void)clearCache
{
    
    
    
}

#pragma mark - Table view

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 18;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 18;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, DCDeviceWidth, 18)];
    view.backgroundColor = [UIColor clearColor];
    return view;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, DCDeviceWidth, 18)];
    view.backgroundColor = [UIColor clearColor];
    return view;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==1) {
        if (indexPath.row==0) {
            [self clearCache];
        }
        if (indexPath.row==2) {
//            [self showLeadingView];
        }
    }
    else if (indexPath.section==0)
    {
        
        XMPPvCardTemp * card= [[DCXMPPTool shareInstance].xmppvCardTempModule myvCardTemp];
        
        DCIMSimpleTextInfoSettingViewController * vc = [[DCIMSimpleTextInfoSettingViewController alloc]init];
        
        vc.defaultText =card.nickname;
        
       
        
       vc.inputedText = ^(NSString* text)
        {
            
            XMPPvCardTemp * card= [[DCXMPPTool shareInstance].xmppvCardTempModule myvCardTemp];
            card.nickname = text;
//
            [[DCXMPPTool shareInstance].xmppvCardTempModule updateMyvCardTemp:card];
        };
        
        
        [self.navigationController pushViewController:vc animated:YES];
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        
        return 80;
    }
    return 40;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section==0) {
        return 1;
    }
    else
    {
        return 3;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0)
    {
        DCMeInfoTableViewCell *cell = [[[NSBundle mainBundle]loadNibNamed:@"DCMeInfoTableViewCell" owner:nil options:nil]lastObject];
        

        
        XMPPvCardTemp * card= [[DCXMPPTool shareInstance].xmppvCardTempModule myvCardTemp];
        
        cell.UserNickName.text = card.nickname;
        cell.UserAccount.text = [[DCXMPPTool shareInstance].xmppStream myJID].user;
        
        if (!card.photo) {
            cell.UserIcon.image = [UIImage imageNamed:@"defaultUserIcon"];
        }
        else
        {
            cell.UserIcon.image=[UIImage imageWithData:card.photo];
        }
        
        
        cell.UserIcon.userInteractionEnabled = YES;
        
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(uploadImage)];
        [cell.UserIcon addGestureRecognizer:tap];
        
        return cell;
    }
    else
    {
        
        DCSettingTableViewCell * cell = [[[NSBundle mainBundle]loadNibNamed:@"DCSettingTableViewCell" owner:nil options:nil]lastObject];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        if (indexPath.row==0) {
            cell.CellTitle.text = @"清除缓存";
        }
        else if(indexPath.row==1)
        {
            
            NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
            NSString *app_Name = [infoDictionary objectForKey:kCFBundleNameKey ];
            cell.CellTitle.text = @"关于我们";
            cell.CellDetail.text = [NSString stringWithFormat:@"关于 %@",app_Name];
        }
        else
        {
            NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
            NSString *app_Name = [infoDictionary objectForKey:kCFBundleNameKey ];
            cell.CellTitle.text = @"产品导读";
            cell.CellDetail.text = @"";
        }
        
        return cell;
        
        
    }
    
    
}

#pragma mark uploadImage
-(void)uploadImage
{
    DCIMImagePicker * picker = [[DCIMImagePicker alloc]init];
    picker.delegate =self;
    
    [picker showActionView];
    
    
}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    
    [picker dismissViewControllerAnimated:YES completion:nil];
    
    UIImage * choseImage = info[UIImagePickerControllerEditedImage];
    
    NSData * data = UIImageJPEGRepresentation(choseImage, 0.5);
    
    UIImage * image = [UIImage imageWithData:data];
    
    image = [DTImageScale imageWithMaxSide:640 sourceImage:image];
    
    if (image) {
        data = UIImageJPEGRepresentation([image fixOrientation], 0.3);
    }
    
    
    if (data) {
        XMPPvCardTemp * card =nil;
        
        card = [[DCXMPPTool shareInstance].xmppvCardTempModule myvCardTemp];
        card.photo = data;
        
        [[DCXMPPTool shareInstance].xmppvCardTempModule updateMyvCardTemp:card];
        
      
    }
    else
    {
        //获取资源失败
    }
    
   

}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
